package com.xmy.cultivate.service.impl;

import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.StudentClassType;
import com.xmy.cultivate.entity.Transcript;
import com.xmy.cultivate.entity.views.GradePositionRequest;
import com.xmy.cultivate.mapper.StudentClassTypeMapper;
import com.xmy.cultivate.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 学生所属班型 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-03-20
 */
@Service
public class StudentClassTypeServiceImpl extends ServiceImpl<StudentClassTypeMapper, StudentClassType> implements IStudentClassTypeService {

    @Autowired
    StudentClassTypeMapper studentClassTypeMapper;

    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    IDictService iDictService;

    @Autowired
    IOrderDetailService iOrderDetailService;

    @Autowired
    IStudentGradeService iStudentGradeService;

    @Autowired
    IGradeService iGradeService;

    @Autowired
    ITranscriptService iTranscriptService;

    @Autowired
    ICourseTypeService iCourseTypeService;

    @Override
    public GradePositionRequest getAutoDivideClassTypeOrScore(Integer yearPart, Integer quarterNum, Long subjectsId, Long studentId, Integer yearClassId){
        Long classTypeId = 1575859909042028546L;//默认提高班
        GradePositionRequest gradePositionRequest = new GradePositionRequest();
        gradePositionRequest.setIsHasData(0);
        gradePositionRequest.setScore(new BigDecimal(0));

        if (yearClassId<=1 && (quarterNum==3 || quarterNum==4)){
            gradePositionRequest.setType(2);
            gradePositionRequest.setClassTypeId(classTypeId);
            gradePositionRequest.setIsHasData(1);
        }else {
            if (subjectsId.equals(1L) && (yearClassId < 3 || (yearClassId == 3 && (quarterNum.equals(3) || quarterNum.equals(4))))){
                gradePositionRequest.setType(2);
                gradePositionRequest.setClassTypeId(classTypeId);
                gradePositionRequest.setIsHasData(1);
            }else if (subjectsId.equals(3L) && (yearClassId < 8 || (yearClassId == 8 && (quarterNum.equals(3) || quarterNum.equals(4))))){
                Quarter quarterLast = iQuarterService.getLastForTranscription(yearPart,quarterNum);
                //物理初二暑假与秋季用上一学期数据的分数计算
                Integer examNum = 3;//期末的
                Transcript transcript = iTranscriptService.getDataTranscript(quarterLast.getYearPart(),quarterLast.getNum(),studentId,2L,examNum);
                if (transcript!=null && transcript.getIsStatistics().equals(1)){
                    gradePositionRequest.setScore(transcript.getScoreEnd());
                    gradePositionRequest.setType(1);
                    gradePositionRequest.setIsHasData(1);
                }
            }else {
                Quarter quarterLast = iQuarterService.getLastForTranscription(yearPart,quarterNum);
                //物理初二暑假与秋季用上一学期数据的分数计算
                Integer examNum = 1;//期中
                //当前学期期中
                Transcript transcript = iTranscriptService.getDataTranscript(yearPart,quarterNum,studentId,subjectsId,examNum);
                if (transcript!=null && transcript.getIsStatistics().equals(1)){
                    gradePositionRequest.setScore(transcript.getScoreEnd());
                    gradePositionRequest.setType(1);
                    gradePositionRequest.setIsHasData(1);
                }else {
                    examNum = 3;
                    transcript = iTranscriptService.getDataTranscript(quarterLast.getYearPart(),quarterLast.getNum(),studentId,subjectsId,examNum);
                    if (transcript!=null && transcript.getIsStatistics().equals(1)){
                        gradePositionRequest.setScore(transcript.getScoreEnd());
                        gradePositionRequest.setType(1);
                        gradePositionRequest.setIsHasData(1);
                    }
                }
            }
        }
        return gradePositionRequest;
    }
}
